package com.aaa.behind.controller;


import com.aaa.behind.service.ITbArticleService;
import com.aaa.entity.TbArticle;
import com.aaa.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author whj
 * @since 2023-02-01
 */
@RestController
@RequestMapping("/tb-article")
public class TbArticleController {

    @Autowired
    private ITbArticleService tbArticleService;

    @PostMapping("getArticleByNavId/{navId}")
    public Result getArticleByNavId(@PathVariable String navId){
        return new Result(tbArticleService.getArticleByNavId(navId));
    }

    @PostMapping("updateArticleByNavId")
    public Result updateArticleByNavId(TbArticle tbArticle){

        if (tbArticle.getGmtCreate() != null){
            tbArticle.setGmtModified(LocalDateTime());
            return new Result(tbArticleService.saveOrUpdate(tbArticle));
        }
        tbArticle.setGmtModified(LocalDateTime());
        tbArticle.setGmtCreate(LocalDateTime());
        tbArticle.setIsDeleted(0);
        return new Result(tbArticleService.saveOrUpdate(tbArticle));
    }

    public String LocalDateTime(){
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dateTimeFormatter.format(now);
        return format;
    }
}

